﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Prsrp
{
    /// <summary>
    /// The editor can run in syncronism with the Game Engine.
    /// </summary>
    public class GameEditor : BaseDisplayable
    {
        private Vector2 mvMouseCurAssetPivotOffset;

        public void HandleKeyboardInputs()
        {
            if (mpSelectedAsset != null)
            {
                if (KeyboardManager.IsKeyPressedOnce(Keys.Delete))
                {
                    mpSelectedAsset.Dispose();
                }
                if (KeyboardManager.IsKeyPressedOnce(Keys.Add))
                {
                    mpSelectedAsset.mfScale += 0.1f;
                }
                if (KeyboardManager.IsKeyPressedOnce(Keys.Subtract))
                {
                    mpSelectedAsset.mfScale -= 0.1f;
                }
            }
        }

        public void HandleMouseInputs()
        {
            if (MouseManager.IsLeftMouseButtonClickedOnce())
            {
                Vector2 vMousePos = MouseManager.GetCurMousePos();
                mpSelectedAsset = null;
                SelectAssetIfIntersect(Game.Engine.CurrentLevel.Enemies, vMousePos);
                SelectAssetIfIntersect(Game.Engine.CurrentLevel.Characters, vMousePos);
                SelectAssetIfIntersect(Game.Engine.CurrentLevel.Collectables, vMousePos);

                if (mpSelectedAsset != null)
                {
                    mvMouseCurAssetPivotOffset = mpSelectedAsset.mvPos - MouseManager.GetCurMousePos();
                    mpSelectedAsset.SetState(BaseStates.kInit);
                }
            }
            if (MouseManager.IsLeftButtonDragging() && mpSelectedAsset != null)
            {
                mpSelectedAsset.mvPos = MouseManager.GetCurMousePos() + mvMouseCurAssetPivotOffset;
                mpSelectedAsset.mvSpeed = new Vector2(0, 0);
            }
        }

        #region Properties

        private BaseAsset mpSelectedAsset;
        public BaseAsset SelectedAsset
        {
            get { return mpSelectedAsset; }
        }

        private bool mbEditorIsEnabled = false;
        public bool Enabled
        {
            get { return mbEditorIsEnabled; }
            set { mbEditorIsEnabled = value; }
        }

        #endregion

        public override void Update()
        {
            if (mbEditorIsEnabled)
            {
                // Update Keyboard
                HandleKeyboardInputs();

                // Update Mouse
                HandleMouseInputs();

                // Update @mpSelectedAsset
                if (mpSelectedAsset != null && mpSelectedAsset.CanDispose())
                {
                    mpSelectedAsset = null;
                }
            }
        }

        public override void Draw()
        {
            if (mbEditorIsEnabled)
            {
                // Display Selected Object Bounds
                if (mpSelectedAsset != null)
                {
                    DrawingFunctions.DrawRectangle(mpSelectedAsset.GetHitBox(), DrawingColors.White);
                }
            }
        }

        public void SelectAssetIfIntersect<T>(List<T> _tabAssets, Vector2 _vMousePos) where T : BaseAsset
        {
            foreach (BaseAsset pCurAsset in _tabAssets)
            {
                if (pCurAsset.IntersectHitBox(_vMousePos))
                {
                    mpSelectedAsset = pCurAsset;
                }
            }
        }
    }
}
